package com.sysmanage.permissions.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sysmanage.common.tools.json.Dic;

@Repository(value="dimTableQueryDao")
public class DimTableQueryDao {
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate = null;
	
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public List<Dic> getDicByTable(String tableName,String valueFiledName,String nameFieldName,String whereStr){
		List<Dic> dicList = new ArrayList<Dic>();
		String querySql = "select "+valueFiledName + ","+nameFieldName + " from "+ tableName;
		if(whereStr!=null&&whereStr.length()>0)
		{
			querySql = querySql + " where " + whereStr;
		}
		
		try {
			List<Dic> list = jdbcTemplate.query(querySql, new RowMapper() {
				public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
					Dic obj = new Dic();
					obj.setValue(rs.getString(1));
					obj.setName(rs.getString(2));
					return obj;
				}
			});
			if(list!=null&&list.size()>0)
			{
				dicList.addAll(list);
			}
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return dicList;
	}
}
